import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  static const platform = MethodChannel('felixyin/battery');

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int battery = 0;
  String test1 = '';


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          padding: EdgeInsets.all(50),
          child: Column(
            children: <Widget>[
              buildBtn(),
              buildText()
            ],
          ),
        ),
      ),
    );
  }

  Widget buildBtn(){
    return RaisedButton(
      child: Text('获取手机电量'),
      onPressed: (){
//        getBatteryInfo();
      testFlutterCallNative();
      },
    );
  }

  Widget buildText(){
    return Text('电池电量：${test1}',style: TextStyle(fontSize: 20),);
  }

  getBatteryInfo() async{
    //调用原生的方法
    final result = await  MyApp.platform.invokeMethod('getBatteryInfo');
    setState(() {
      battery = result;
    });
  }

  testFlutterCallNative() async{
    //传参数给Native
    final result = await MyApp.platform.invokeMethod('test',['aaaa','bbbb',2222]);
    setState(() {
      test1 = result;
      print('原生回传参数 ${test1}');
    });
  }
}

